<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;

class Order extends Controller
{
    public $user_id = 0;
    public $system_id = 0;
    public $user = array();
    public $check_date = 0;

    public function _initialize() {      
        parent::_initialize();
        if(session('?user'))
        {
            $user = session('user');
            $user = Db::table('users')->where("id", $user['id'])->find();
            session('user',$user);            
            $this->user = $user;
            $this->user_id = $user['id'];
            $this->system_id = $user['system_id'];
            $this->assign('user',$user); //存储用户信息
            $this->assign('user_id',$this->user_id);
        }
    }
    
    /**
     * 
     * @return json
     */
    public function order_list() {
        $user_id = $this->user_id;
        $where_arr = array();
        $business_id = Db::table('business')->where('business_id',$user_id)->find();
        if(!$business_id){
            $date = array('code'=>20020,'msg'=>'非商户','data'=>'');
            exit(json_encode($date));
        }
        $where['business_id'] = $business_id['id'];
        $begin = strtotime(input('add_time_begin'));
        $end = strtotime(input('add_time_end'));
        if($begin && $end){
            $where_arr['createtime'] = array('between',"$begin,$end");
        }else{
            $begin = date('Y-m-d', strtotime('-7 days'));
            $end = date('Y-m-d');
            input('add_time') != '' ? $where_arr['createtime'] = array('BETWEEN',"$begin,$end") : false;
        }
        
        input('order_sn') != '' ? $where_arr['batchcode'] = input('order_sn') : false;
        input('product_name') != '' ? $where_arr['product_name'] = input('product_name') : false;
        input('status') != '' ? $where_arr['status'] = input('status') : false;  // 订单状态  根据线上/线下变化
        input('shipping_code') != '' ? $where_arr['shipping_code'] = input('shipping_code') : false;
        input('pay_code') != '' ? $where_arr['pay_code'] = input('pay_code') : false;
        input('type') != '' ? $where_arr['type'] = input('type') : false;   // 订单类型  线上/线下
        
        $row = Db::table('orders')->where($where_arr)->paginate();
        if($row){
            $date = array('code'=>666,'msg'=>'订单列表获取成功','data'=>$row);
        } else {
            $date = array('code'=>20038,'msg'=>'订单列表获取失败','data'=>'');    
        }
        return $date;
    }
    
    
    public function order_info() {
        $where = array();
        $user_id = $this->user_id;
        $business_id = Db::table('business')->where('business_id',$user_id)->find();
        if(!$business_id){
            $date = array('code'=>20020,'msg'=>'非商户','data'=>'');
            exit(json_encode($date));
        }
        $where['business_id'] = $business_id['id'];
        input('order_sn') ? $where['batchcode'] = input('order_sn') : exit('订单号不能为空');
        $row = Db::table('orders_info')->where($where)->paginate();
        if($row){
            $date = array('code'=>666,'msg'=>'订单详情获取成功','data'=>$row);
        } else {
            $date = array('code'=>20039,'msg'=>'订单详情获取失败','data'=>'');    
        }
        return $date;
    }
    
}

